package com.stylefeng.guns.modular.alarm.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.modular.alarm.service.impl.DoorAlarmRecordServiceImpl;
import com.stylefeng.guns.modular.system.model.Record;
import com.stylefeng.guns.modular.system.warpper.LogWarpper;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.DoorAlarmRecord;
import com.stylefeng.guns.modular.alarm.service.IDoorAlarmRecordService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 报警日志控制器
 *
 * @author fengshuonan
 * @Date 2018-10-17 10:52:16
 */
@Controller
@RequestMapping("/doorAlarmRecord")
public class DoorAlarmRecordController extends BaseController {
    Logger logger = LoggerFactory.getLogger(DoorAlarmRecordController.class);
    private String PREFIX = "/alarm/doorAlarmRecord/";


    @Autowired
    private IDoorAlarmRecordService doorAlarmRecordService;

    /**
     * 跳转到报警日志首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "doorAlarmRecord.html";
    }

    /**
     * 跳转到添加报警日志
     */
    @RequestMapping("/doorAlarmRecord_add")
    public String doorAlarmRecordAdd() {
        return PREFIX + "doorAlarmRecord_add.html";
    }

    /**
     * 跳转到修改报警日志
     */
    @RequestMapping("/doorAlarmRecord_update/{doorAlarmRecordId}")
    public String doorAlarmRecordUpdate(@PathVariable Integer doorAlarmRecordId, Model model) {
        DoorAlarmRecord doorAlarmRecord = doorAlarmRecordService.selectById(doorAlarmRecordId);
        model.addAttribute("item",doorAlarmRecord);
        LogObjectHolder.me().set(doorAlarmRecord);
        return PREFIX + "doorAlarmRecord_edit.html";
    }


    /**
     * 获取报警日志列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list( String name,String cardNo,String status,String doorNumber,String inOrGo,String beginTime,String endTime) throws Exception{
        System.out.println();
        if (name==null&&cardNo==null&&status==null&&beginTime==null&&endTime==null){
            return doorAlarmRecordService.selectList(null);
        }else {
            EntityWrapper<DoorAlarmRecord> wrapper = new EntityWrapper<>();
            if (name!=null){
                wrapper.like("name",name);
            }
            if (cardNo!=null){
                wrapper.like("cardno",cardNo);
            }
            if (status!=null){
                wrapper.like("note1",status);
            }
            if (doorNumber!=null){
                wrapper.like("note2",doorNumber);
            }
            if (inOrGo!=null){
                wrapper.like("note3",inOrGo);
            }
            if (beginTime!=null&&beginTime!=""&&endTime!=null&&endTime!=""){
                wrapper.between("time",beginTime,endTime);
            }
            return doorAlarmRecordService.selectList(wrapper);
        }
    }
    /**
     * 获取报警日志3条
     *
     * */
    @RequestMapping(value = "/blacklist")
    @ResponseBody
   /* public Object list(@RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String carNumber, @RequestParam(required = false) String logType) {
        Page<DoorAlarmRecord> page = new PageFactory<DoorAlarmRecord>().defaultPage();
        page.setSize(2);
        List<Map<String, Object>> result = doorAlarmRecordService.getAlarmLog(page, beginTime, endTime, carNumber, logType);
        page.setRecords((List<DoorAlarmRecord>) new LogWarpper(result).warp());
        return super.packForBT(page);
    }*/

        public Object listAlarm(){
           Page<DoorAlarmRecord> page =  doorAlarmRecordService.selectPage(new Page<DoorAlarmRecord>(1,3),new EntityWrapper<DoorAlarmRecord>().orderBy("time",false));
//            return new PageInfoBT<DoorAlarmRecord>(page);
            return page.getRecords();
    }


    /**
     * 新增报警日志
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DoorAlarmRecord doorAlarmRecord) {
        doorAlarmRecordService.insert(doorAlarmRecord);
        return SUCCESS_TIP;
    }

    /**
     * 删除报警日志
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer doorAlarmRecordId) {
        doorAlarmRecordService.deleteById(doorAlarmRecordId);
        return SUCCESS_TIP;
    }

    /**
     * 修改报警日志
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DoorAlarmRecord doorAlarmRecord) {
        doorAlarmRecordService.updateById(doorAlarmRecord);
        return SUCCESS_TIP;
    }

    /**
     * 报警日志详情
     */
    @RequestMapping(value = "/detail/{doorAlarmRecordId}")
    @ResponseBody
    public Object detail(@PathVariable("doorAlarmRecordId") Integer doorAlarmRecordId) {
        return doorAlarmRecordService.selectById(doorAlarmRecordId);
    }
    /*
    * 报警日志列表清空
    * */
    /**
     * 清空
     */
    @RequestMapping(value = "/deleteAll")
    @ResponseBody
    public Object deleteAll() {
        doorAlarmRecordService.deleteAll();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        return SUCCESS_TIP;
    }

    /**
     * 下载打卡信息
     */
    @RequestMapping(value = "/download")
    @ResponseBody
    public void download(HttpServletResponse response) throws IOException {
        System.out.print("-===================================下载");
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("打卡信息表");

//        List<CarInfo> listCar = carInfoService.getCarInfo(info.getPlateNumber(),info.getType(),info.getStatus());
        List<DoorAlarmRecord> recordList = doorAlarmRecordService.selectList(null);
        String fileName = "alarmRecord"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "编号", "名称", "卡号", "开门状态","进出","门号","刷卡时间","预留1","预留2"};//headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);//在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (DoorAlarmRecord record : recordList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(record.getId());
            row1.createCell(1).setCellValue(record.getName());
            row1.createCell(2).setCellValue(record.getCardno());
            row1.createCell(3).setCellValue(record.getOpenDoorStatus());
            row1.createCell(4).setCellValue(record.getDoorNumber());
            row1.createCell(5).setCellValue(record.getInOut());
            row1.createCell(6).setCellValue(record.getTime());
            row1.createCell(7).setCellValue(record.getNote1());
            row1.createCell(8).setCellValue(record.getNote2());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        System.out.println("测试导出excel");
        workbook.write(response.getOutputStream());
    }
}
